package cn.sangyy.basic.exception;



import cn.sangyy.basic.common.code.CodeEnum;

import java.util.List;

/**
 * @author SangYY
 * @date: 2023/03/30/15:17
 */
public class CarAssert {

    public final static String DEFAULT_MESSAGE = "非法请求，请输入相关参数再操作";

    //有默认值的对象不为空的提示
    public static void assertNotNull(Object obj){
        if(obj == null){
            throw new BusinessException(DEFAULT_MESSAGE);
        }
    }

    //断言对象不为空，为空抛自定义异常
    public static void assertNotNull(Object obj,String message){
        if(obj == null){
            throw new BusinessException(message);
        }
    }

    public static void assertNotNull(Object obj,CodeEnum codeEnum){
        if(obj == null){
            throw new BusinessException(codeEnum.getMessage(),codeEnum.getCode());
        }
    }

    //断言对象为空，不为空抛自定义异常
    public static void assertIsNull(Object obj,String message){
        if(obj != null){
            throw new BusinessException(message);
        }
    }

    //断言String为空
    public static void assertStrNotBlank(String str,String message){
        if(str == null || str.trim().equals("")){
            throw new BusinessException(message);
        }
    }

    public static void assertStrNotBlank(String str, CodeEnum codeEnum){
        if(str == null || str.trim().equals("")){
            throw new BusinessException(codeEnum.getMessage(),codeEnum.getCode());
        }
    }

    //断言String不为空
    public static void assertStrBlank(String str,String message){
        if(str != null || !str.trim().equals("")){
            throw new BusinessException(message);
        }
    }

    //断言List不为空，为空抛自定义异常
    public static void assertListNotNull(List<?> objs, String message){
        if(objs == null || objs.size() < 1){
            throw new BusinessException(message);
        }
    }

    //断言List不为空，为空抛自定义异常
    public static void assertListNotNull(List<?> objs, CodeEnum codeEnum){
        if(objs == null || objs.size() < 1){
            throw new BusinessException(codeEnum.getMessage(),codeEnum.getCode());
        }
    }

    //断言List为空，不为空抛自定义异常
    public static void assertListIsNull(List<?> objs,String message){
        if(objs.size() > 0){
            throw new BusinessException(message);
        }
    }

    //断言List为空，不为空抛自定义异常
    public static void assertListIsNull(List<?> objs,CodeEnum codeEnum){
        if(objs.size() > 0){
            throw new BusinessException(codeEnum.getMessage(),codeEnum.getCode());
        }
    }

    //断言对象参数是否正确
    public static void assertObjectIsTrue(CodeEnum codeEnum){
        throw new BusinessException(codeEnum.getMessage(),codeEnum.getCode());

    }

    //断言字符串相等 message为字符串
    public static void assertStrEquals(String str1, String str2, String message){
        assertStrNotBlank(str1,message);
        assertStrNotBlank(str2,message);
        if(!str1.equals(str2)){
            throw new BusinessException(message);
        }
    }

    //断言字符串相等 传入枚举类型
    public static void assertStrEquals(String str1, String str2, CodeEnum codeEnum){
        assertStrNotBlank(str1,codeEnum);
        assertStrNotBlank(str2,codeEnum);
        if(!str1.equals(str2)){
            throw new BusinessException(codeEnum.getMessage(),codeEnum.getCode());
        }
    }

    //断言字符串相等忽略String大小写
    public static void assertStrEqIgnoreCase(String str1,String str2,String message){
        assertStrNotBlank(str1,message);
        assertStrNotBlank(str2,message);
        if(!str1.equalsIgnoreCase(str2)){
            throw new BusinessException(message);
        }
    }
}
